import {BasePage, BasePageData} from "../common/core/BasePage";
import {field} from "../../modules/coreModule/data/DataLoader";
import {Banner, bannerRepo} from "../../modules/miscModule/data/Banner";
import {Notice, noticeRepo} from "../../modules/messageModule/data/Notice";
import {page, pageFunc} from "../common/PageBuilder";
import {AuthPage} from "../common/partPages/UserPage";
import {waitForDataLoad} from "../../modules/coreModule/managers/DataManager";
import {pageMgr} from "../../modules/coreModule/managers/PageManager";
import {ExamPage} from "../exam/ExamPage";
import {RecruitPage} from "../recruit/RecruitPage";
import {alertMgr, handleError} from "../../modules/coreModule/managers/AlertManager";
import {RoleType} from "../../modules/userModule/data/Role";
import {CampPage} from "../camp/CampPage";
import {BaseData, PK} from "../../modules/coreModule/data/BaseData";
import {BannerDetailPage} from "../bannerDetail/BannerDetailPage";
import {AppConfig, appConfigRepo} from "../../modules/coreModule/data/AppConfig";
import {ShareAppPage, ShareTimelinePage} from "../common/partPages/SharePage";
import {userMgr} from "../../modules/userModule/managers/UserManager";

class Data extends BasePageData {
	@field([Banner])
	banners: Banner[] = []
	@field(AppConfig)
	appConfig: AppConfig
	@field(Boolean)
	showShopQRCode: boolean = false;

	@field(Notice)
	notice: Notice

	@field(String)
	province: string
	@field(String)
	city: string
}

@page("homepage", "主页")
export class HomePage extends BasePage<Data> {

	data = new Data();

	/**
	 * 部分页
	 */
	public userPage: AuthPage = new AuthPage(
		true, false, false);
	public shareAppPage = new ShareAppPage();
	public shareTimelinePage = new ShareTimelinePage();

	onLoad(e) {
		super.onLoad(e);
		this.loadData().then()

		userMgr().savedInviteCode = this.getExtra("code");
	}

	@waitForDataLoad
	private async loadData() {
		await this.setData({
			banners: bannerRepo().find(),
			appConfig: appConfigRepo().data,
			notice: noticeRepo().findOne(),
			province: "河南省",
		})
		userMgr().location = {
			province: this.data.province,
			city: this.data.city
		}
	}

	// region 页面跳转

	@pageFunc
	public toRecruit() {
		pageMgr().push(RecruitPage).then();
	}

	@pageFunc
	@handleError
	public toExam() {
		this.userPage.ensureRole(RoleType.Instructor,
			"只有教官才需要考核！");
		pageMgr().push(ExamPage).then();
	}

	@pageFunc
	public toCamp() {
		pageMgr().push(CampPage).then();
		// alertMgr().showAlert("功能暂未开放，敬请期待");
	}

	@pageFunc
	public toShop() {
		// alertMgr().showAlert("功能暂未开放，敬请期待");
		this.setData({showShopQRCode: true})
	}

	@pageFunc
	public onQRCodeClose() {
		this.setData({showShopQRCode: false})
	}
	// endregion

	// endregion

	@pageFunc
	public bindRegionChange(e) {
		this.setData(userMgr().location = {
			province: e.detail.value[0],
			city: e.detail.value[1],
		})
	}

	@pageFunc
	public toBannerDetail(e) {
		const bannerId: PK = e.currentTarget.dataset.banner._id;
		pageMgr().push(BannerDetailPage, { bannerId } as any).then();
	}
	@pageFunc
	public toNoticeDetail(e) {
		pageMgr().push(BannerDetailPage,
			{ noticeId: this.data.notice._id } as any).then();
	}
}
